package com.njym.controller;

/**
 * @program: chpp
 * @description:
 * @author: Mr.LI
 * @create: 2018-08-21 13:52
 **/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysApplyChannel;
import com.njym.entity.SysUser;
import com.njym.service.ApplyChannelService;
import com.njym.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@PermInfo(value = "应用渠道模块", pval = "a:sys:applychannel")
@RestController
@RequestMapping("/apply_channel")
public class ApplyChannelController {

    private static final Logger log = LoggerFactory.getLogger(ApplyChannelController.class);

    @Autowired
    private ApplyChannelService applyChannelService;

    @PermInfo("查询所有应用渠道")
    @RequiresPermissions("a:sys:applychannel:query")
    @PostMapping("/query")
    public Json query(@RequestBody String body) {
        String oper = "query applychannel";
//        log.info("{}, body: {}", oper, body);
//        JSONObject json = JSON.parseObject(body);
//        Page<SysApplyChannel> page = applyChannelService.queryApplychannel(PageUtils.getPageParam(json));
//        return Json.succ(oper).data("page", page);
        JSONObject json = JSON.parseObject(body);
        int current = json.getIntValue("current");
        int size = json.getIntValue("size");
        if (current == 0) current = 1;
        if (size == 0) size = 10;
        Wrapper<SysApplyChannel> queryParams = new EntityWrapper<>();
        queryParams.orderBy("sort_no", true);
        queryParams.eq("deleted", 0);
        Subject currentUser = SecurityUtils.getSubject();
        SysUser user = (SysUser) currentUser.getPrincipal();
        if(!user.getOrgId().equals("0")&& org.apache.commons.lang.StringUtils.isNotEmpty(user.getOrgId())){
            queryParams.eq("org_id",user.getOrgId());
        }
        String channelNames = json.getString("channelName");
//        System.out.println(channelNames);
        if (channelNames != null && StringUtils.isNotEmpty(channelNames)) {
            queryParams.like("channel_name", channelNames);
        }
        Page<SysApplyChannel> page = applyChannelService.selectPage(new Page<>(current, size), queryParams);
        return Json.succ(oper).data("page", page);
    }

    @PermInfo("添加应用")
    @RequiresPermissions("a:sys:applychannel:add")
    @PostMapping("/add")
    public Json add(@RequestBody String body) {
        String oper = "add applychannel";
        log.info("{}, body: {}", oper, body);

        SysApplyChannel sysApplyChannel = JSON.parseObject(body, SysApplyChannel.class);

        SysApplyChannel ApplyChannel = applyChannelService.selectOne(new EntityWrapper<SysApplyChannel>().eq("channel_code", sysApplyChannel.getChannelCode()));
        if (ApplyChannel != null) {
            return Json.fail(oper, "应用代码已注册");
        }
        SysApplyChannel ApplyChannel2 = applyChannelService.selectOne(new EntityWrapper<SysApplyChannel>().eq("channel_code", sysApplyChannel.getChannelName()));
        if (ApplyChannel2 != null) {
            return Json.fail(oper, "应用名称已注册");
        }
        boolean success =  applyChannelService.insert(sysApplyChannel);

        return Json.result(oper, success)
                .data("id",sysApplyChannel.getId());
    }

    @PermInfo("修改应用")
    @RequiresPermissions("a:sys:applychannel:update")
    @PostMapping("/update")
    public Json update(@RequestBody String body) {
        String oper = "update applychannel";
        log.info("{}, body: {}", oper, body);
        SysApplyChannel sysApplyChannel = JSON.parseObject(body, SysApplyChannel.class);

        boolean success =  applyChannelService.updateById(sysApplyChannel);

        return Json.result(oper, success)
                .data("id",sysApplyChannel.getId());

    }
    @PermInfo("删除系统用户")
    @RequiresPermissions("a:sys:applychannel:delete")
    @DeleteMapping
    public Json delete(@RequestBody String body) {

        String oper = "delete applychannel";
        log.info("{}, body: {}",oper,body);
        SysApplyChannel sysApplyChannel = JSON.parseObject(body, SysApplyChannel.class);


        JSONObject jsonObj = JSON.parseObject(body);
        String id = jsonObj.getString("id");
        boolean success =  applyChannelService.deleteById(id);

        return Json.result(oper, success)
                .data("id",sysApplyChannel.getId());
    }




}
